//
//  ADScrollView.m
//  ZHIFUBAO
//
//  Created by 高新川 on 15/6/17.
//  Copyright (c) 2015年 高新川. All rights reserved.
//

#import "ADScrollView.h"

@implementation ADScrollView

- (void)dealloc {
    // 关闭定时器并释放
    [_timer invalidate];
    _timer = nil;
}

- (id)init {
    self = [super init];
    if (self) {
        self.ads = [NSMutableArray arrayWithObjects:@"ad01.png", @"ad02.png", @"ad03.jpg", nil];
        
        // scroll的分页设置
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.contentSize = CGSizeMake(3 * 320, 100.0);
        
        NSInteger kCount = [_ads count];
        self.images = [[NSMutableArray alloc] initWithCapacity:3];
        // 初始化三个切换页
        for (int i = 0; i < 3; i++) {
            UIImageView *view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[_ads objectAtIndex:(i+kCount-1)%kCount]]];
            view.frame = CGRectMake(i*320, 0, 320, 100);
            [self addSubview:view];
            [self.images addObject:view];
        }
        _nowImageIndex = 0;
        self.bounds = CGRectMake(320, 0, 320, 100);
        _curPoint = self.bounds.origin;   // 当前左上角坐标
        
        // 初始化定时器
        _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(scrollTimer) userInfo:nil repeats:YES];
        //self.adDelegate = nil;
    }
    return self;
}

#pragma mark - 定时器
- (void)scrollTimer {
    // 自动播放
    [self scrollRectToVisible:CGRectMake(640, 0, 320, 100) animated:YES];
}

#pragma mark - 设置显示第几页
- (void)changeToImage:(NSUInteger)index {
    _nowImageIndex = index;         // 当前视图下标切换过去
    
    // 设置三个view的显示
    self.bounds = CGRectMake(320, 0, 320, 100);
    ((UIImageView *)[self.images objectAtIndex:2]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImageNext]]];     // 加载下一张视图
    ((UIImageView *)[self.images objectAtIndex:1]).image = [UIImage imageNamed:[self.ads objectAtIndex:_nowImageIndex]];     // 加载当前窗口视图
    ((UIImageView *)[self.images objectAtIndex:0]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImagePre]]];     // 加载上一个窗口视图
}

#pragma mark - 判断左滑 右滑 实现滑动效果
- (NSInteger)scrollToPoint:(CGPoint)point {
    NSInteger dictionary = 0;
    if (point.x-_curPoint.x == 320.0 || point.x-_curPoint.x == -640.0) {  // 右滑
        _nowImageIndex = [self getImageNext];    // 获取当前图片下标
        dictionary = 1;
    }
    else if (0 != point.x - _curPoint.x){       // 左滑
        _nowImageIndex = [self getImagePre];    // 获取当前图片下标
        dictionary = -1;
    }
    
    // 始终显示的是中间视图，只不过各个视图加载的图片变了
    self.bounds = CGRectMake(320, 0, 320, 100);
    ((UIImageView *)[self.images objectAtIndex:2]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImageNext]]];     // 加载下一张视图
    ((UIImageView *)[self.images objectAtIndex:1]).image = [UIImage imageNamed:[self.ads objectAtIndex:_nowImageIndex]];     // 加载当前窗口视图
    ((UIImageView *)[self.images objectAtIndex:0]).image = [UIImage imageNamed:[self.ads objectAtIndex:[self getImagePre]]];     // 加载上一个窗口视图
    
    return dictionary;       // 如果是右滑  返回yes  左滑返回no
}

#pragma mark - 获取图片下标
- (NSInteger)getImageNext {
    if (_nowImageIndex == [_ads count] - 1) {
        return 0;
    }
    return _nowImageIndex + 1;
}

- (NSInteger)getImagePre {
    if (0 == _nowImageIndex) {
        return [_ads count]-1;
    }
    return _nowImageIndex-1;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
